package com.qianhui.dao;

import com.qianhui.common.IResult;
import com.qianhui.pojo.ManagerModule;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * A data access object (DAO) providing persistence and search support for
 * ManagerModule entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 *
 * @author MyEclipse Persistence Tools
 * @see com.qianhui.pojo.ManagerModule
 */
public class ManagerModuleDAO extends BaseHibernateDAO
{
    private static final Logger log = LoggerFactory.getLogger(ManagerModuleDAO.class);
    // property constants
    public static final String MANAGER_NAME = "managerName";
    public static final String MODULE_NAME = "moduleName";

    public ManagerModule save(ManagerModule transientInstance)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            getSession().saveOrUpdate(transientInstance);
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return transientInstance;
    }

    public List<ManagerModule> saveList(List<ManagerModule> list)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            for (ManagerModule temp : list)
            {
                getSession().saveOrUpdate(temp);
            }
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return list;
    }

    public int delete(ManagerModule persistentInstance)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            getSession().delete(persistentInstance);
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return IResult.DELETE_EXCEPTION;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return IResult.DELETE_SUCCESS;
    }

    public ManagerModule findById(java.lang.String id)
    {
        log.debug("getting ManagerModule instance with id: " + id);
        try
        {
            ManagerModule instance = (ManagerModule) getSession().get("com.qianhui.pojo.ManagerModule", id);
            return instance;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List findByProperty(String propertyName, Object value)
    {
        log.debug("finding ManagerModule instance with property: " + propertyName + ", value: " + value);
        try
        {
            String queryString = "from ManagerModule as model where model." + propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);
            return queryObject.list();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List findByModuleName(Object moduleName)
    {
        return findByProperty(MODULE_NAME, moduleName);
    }

    public List getListByManagerName(String managerName)
    {
        try
        {
            String sql = "from ManagerModule m where m.managerName=:managerName order by m.moduleAddTime asc";
            Query query = getSession().createQuery(sql);
            query.setParameter("managerName", managerName);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return tempList;
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public ManagerModule findByManagerNameAndModuleName(String managerName, String moduleName)
    {
        try
        {
            String sql = "from ManagerModule m where m.managerName=:managerName and m.moduleName=:moduleName";
            Query query = getSession().createQuery(sql);
            query.setParameter("managerName", managerName);
            query.setParameter("moduleName", moduleName);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return (ManagerModule) tempList.get(0);
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

}